package com.lenovo.manufacture.module;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.EntryXComparator;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.adapter.MyAdapter;
import com.lenovo.manufacture.annotation.Layout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Module9 extends BaseActivity {

    @BindView(R.id.tv_car1)
    TextView tvCar1;
    @BindView(R.id.tv_car2)
    TextView tvCar2;
    @BindView(R.id.tv_car3)
    TextView tvCar3;
    @BindView(R.id.lc_chart)
    LineChart lcChart;
    @BindView(R.id.tv_sort1)
    BootstrapLabel tvSort1;
    @BindView(R.id.tv_sort2)
    BootstrapLabel tvSort2;
    @BindView(R.id.tv_sort3)
    BootstrapLabel tvSort3;
    @BindView(R.id.rv_module8)
    RecyclerView rvModule8;
    @BindView(R.id.tv_price_sum)
    BootstrapLabel tvPriceSum;

    private List<Map<String, String>> sellOutLogList;
    private List<Map<String, String>> cars;
    private SimpleDateFormat simpleDateFormat;

    @Layout(value = R.layout.activity_module9,title = "销售报表")
    protected void init() {
        simpleDateFormat = new SimpleDateFormat("MM月dd日");
        async(apiService.getSellOutLog(), data -> {
            sellOutLogList = data.getData();
            async(apiService.getCarType(), data1 -> {
                cars = data1.getData();
                for (int i = 0; i < cars.size(); i++) {
                    Map<String, String> car = cars.get(i);
                    Integer reduce = sellOutLogList.stream()
                            .filter(sellOutLog -> car.get("id").equals(sellOutLog.get("carTypeId")))
                            .map(sellOutLog -> Integer.parseInt(sellOutLog.get("num")) * Integer.parseInt(sellOutLog.get("gold")))
                            .reduce(0, Integer::sum);
                    setTv(i, car.get("carTypeName"), reduce);
                }
                Integer num = sellOutLogList.stream().map(sellOutLog -> Integer.parseInt(sellOutLog.get("gold")) * Integer.parseInt(sellOutLog.get("num")))
                        .reduce(0, Integer::sum);
                tvPriceSum.setText("汽车总销售额（元）\n" + decimalFormat.format(num));
                initChart();
                initList();
            });
        });
    }

    public void initChart() {
        List<Entry> entries = new ArrayList<>();
        sellOutLogList.forEach(sellOutLog -> {
            entries.add(new Entry(Float.parseFloat(sellOutLog.get("time")), Float.parseFloat(sellOutLog.get("gold")) * Float.parseFloat(sellOutLog.get("num"))));
        });
        Collections.sort(entries,new EntryXComparator());
        LineDataSet lineDataSet = new LineDataSet(entries, "销售表");
        LineData lineData = new LineData(lineDataSet);
        lcChart.setData(lineData);
        lcChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lcChart.getXAxis().setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return simpleDateFormat.format(new Date(((long) value * 1000)));
            }
        });
        lcChart.invalidate();
    }

    private void initList() {
        List<String> strings = new ArrayList<>();
        sellOutLogList.forEach(sellOutLog -> {
            Map<String, String> car1 = cars.stream().filter(car -> car.get("id").equals(sellOutLog.get("carTypeId"))).collect(Collectors.toList()).get(0);
            strings.add(car1.get("carTypeName"));
            strings.add(sellOutLog.get("gold"));
            strings.add(simpleDateFormat.format(new Date(Long.parseLong(sellOutLog.get("time")) * 1000)));
            strings.add(sellOutLog.get("num"));
        });
        rvModule8.setLayoutManager(new GridLayoutManager(this, 4));
        rvModule8.setAdapter(new MyAdapter(strings));
    }

    private void setTv(int index, String name, Integer num) {
        TextView textView = null;
        switch (index) {
            case 0:
                textView = tvCar1;
                break;
            case 1:
                textView = tvCar2;
                break;
            case 2:
                textView = tvCar3;
                break;
        }
        textView.setText(String.format("%s型汽车销售额\n%s", name, decimalFormat.format(num)));
    }

    @OnClick({R.id.tv_sort1, R.id.tv_sort2, R.id.tv_sort3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sort1:
                sort(sellOutLogList, "gold", tvSort1, "价格");
                break;
            case R.id.tv_sort2:
                sort(sellOutLogList, "time", tvSort2, "时间");
                break;
            case R.id.tv_sort3:
                sort(sellOutLogList, "num", tvSort3, "数量");
                break;
        }
        initList();
    }
}